<template>
  <!-- 添加或修改菜单对话框 -->
  <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
    <el-form ref="formData" :model="formData" :rules="rules" label-width="80px">
      <el-form-item label="用户名" prop="loginName">
        <el-input v-model="formData.loginName" placeholder="用户名" size="small" />
      </el-form-item>
      <el-form-item label="昵称" prop="userName">
        <el-input v-model="formData.userName" placeholder="起一个名字" size="small" />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select
          v-model="formData.status"
          size="small"
          :disabled="$store.getters.userId == formData.userId"
        >
          <el-option label="正常" value="0"></el-option>
          <el-option label="禁用" value="1"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="部门" prop="deptIds">
        <el-cascader
          filterable
          v-model="formData.deptIds"
          :options="deptTree"
          :show-all-levels="false"
          placeholder="部门"
          :props="{ value: 'value', label: 'title', checkStrictly: true }"
          @change="getdeptIds"
          :key="cascaderKey"
          size="small"
        ></el-cascader>
      </el-form-item>
      <el-form-item label="描述" prop="remark">
        <el-input
          type="textarea"
          :autosize="{ minRows: 3, maxRows: 5}"
          placeholder="请输入描述"
          v-model="formData.remark"
          size="small"
        ></el-input>
      </el-form-item>
      <el-form-item label="拥有角色" prop="roleIds">
        <el-select v-model="formData.roleIds" multiple collapse-tags filterable size="small">
          <el-option
            :label="item.roleName"
            :value="item.roleId"
            v-for="item in roleAll"
            :key="item.roleId"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="拥有岗位" prop="postIds">
        <el-select v-model="formData.postIds" multiple collapse-tags filterable size="small">
          <el-option
            :label="item.postName"
            :value="item.postId"
            v-for="item in postAll"
            :key="item.postId"
          ></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button
        type="primary"
        @click="submitForm('formData')"
        :disabled="loading"
        :icon="loading?'el-icon-loading':'el-icon-circle-plus-outline'"
        size="mini"
      >确 定</el-button>
      <el-button @click="open = false" size="mini">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { getRoleAll, saveUser, listPost, getUser } from "@/api/system";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
const ERR_OK = 200;
export default {
  components: {
    Treeselect
  },
  props: {
    deptTree: Array
  },
  data() {
    return {
      formData: {},
      rules: {
        loginName: [
          {
            required: true,
            message: "请输入用户名",
            trigger: "blur"
          }
        ],
        userName: [
          {
            required: true,
            message: "请输入昵称",
            trigger: "blur"
          }
        ],
        status: [
          {
            required: true,
            message: "请选择状态",
            trigger: "change"
          }
        ],
        roleIds: [
          {
            required: true,
            message: "请选择角色",
            trigger: "change"
          }
        ],
        postIds: [
          {
            required: true,
            message: "请选择岗位",
            trigger: "change"
          }
        ]
      },
      title: "",
      loading: false,
      open: false,
      roleAll: [],
      postAll: [],
      cascaderKey: 1
    };
  },
  created() {
    this.loadRoleAll();
    this.loadListPost();
  },
  methods: {
    add() {
      ++this.cascaderKey;
      this.title = "新增用户";
      this.open = true;
      this.formData = {
        loginName: "",
        userName: "",
        status: "0",
        deptIds: [],
        remark: "",
        roleIds: [],
        postIds: [],
        userId: 0
      };
    },
    loadRoleAll() {
      getRoleAll().then(res => {
        this.roleAll = res.data.rows;
      });
    },
    loadListPost() {
      listPost().then(res => {
        this.postAll = res.data.rows;
      });
    },
    edit(row) {
      this.title = "编辑用户";
      ++this.cascaderKey;
      let formData = JSON.parse(JSON.stringify(row));
      let deptId = [];
      this.formData = formData;
      getUser(row.userId).then(res => {
        this.formData.postIds = res.data.postIds;
        this.formData.parentId = res.data.data.parentId;
        res.data.data.dept.ancestors
          .split(",")
          .forEach(item => {
            if (item) {
              deptId.push(Number(item));
            }
          });
        deptId.push(res.data.data.deptId);
        console.log(deptId,res.data.data)
        this.formData.deptIds = deptId;
      });
      this.open = true;
    },
    getdeptIds(value) {
      this.formData.deptIds;
      console.log(value);
    },
    submitForm(formName) {
      if (!this.formData.deptIds > 0) {
        this.$message.warning("请选择部门");
        return;
      }
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.loading = true;
          this.formData.deptId = this.formData.deptIds[
            this.formData.deptIds.length - 1
          ];
          saveUser(this.formData).then(res => {
            this.loading = false;
            if (res.data.code === ERR_OK) {
              this.$message.success(res.data.msg);
              this.$emit("ok");
              this.open = false;
            } else {
              this.$message.error(res.data.msg);
            }
          });
        } else {
          return false;
        }
      });
    }
  }
};
</script>

<style lang="scss" scoped>
.el-select,
.el-cascader {
  width: 100%;
}
</style>